package com.cdxy.service.impl;

import com.cdxy.dmr.dataObject.AxUserDO;
import com.cdxy.dmr.repository.AxUserRepository;
import com.cdxy.enums.BizErrorEnum;
import com.cdxy.enums.KaptchaEnum;
import com.cdxy.model.dto.base.exception.BusinessException;
import com.cdxy.model.dto.base.exception.enums.CommonErrorEnum;
import com.cdxy.model.dto.req.ForgetPasswordReq;
import com.cdxy.model.dto.req.ModifyPasswordReq;
import com.cdxy.model.dto.req.SendMobileCodeReq;
import com.cdxy.model.dto.req.SetPasswordReq;
import com.cdxy.service.IPasswordOptService;
import com.cdxy.service.ISmsService;
import com.cdxy.util.BeanUtil;
import com.cdxy.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: tangy
 * @Date: 2020/3/10 11:09
 */
@Service
@Slf4j
public class PasswordOptServiceImpl implements IPasswordOptService {

    @Autowired
    private ISmsService smsService;

    @Autowired
    private AxUserRepository axUserRepository;

    @Override
    public void setPassword(SetPasswordReq setPasswordReq) {
        Integer axUid = setPasswordReq.getAxUid();
        String password = setPasswordReq.getPassword();

        AxUserDO axUserDO = axUserRepository.getById(axUid);
        if (axUserDO == null) {
            throw new BusinessException(BizErrorEnum.LOGIN_ACCOUNT_NOT_EXIST);
        }

        if (StringUtils.isNotBlank(axUserDO.getPassword())) {
            throw new BusinessException(BizErrorEnum.HAD_SET_PASSWORD);
        }

        AxUserDO axUserUpdateDO = new AxUserDO();
        axUserUpdateDO.setAxUid(axUid);
        axUserUpdateDO.setPassword(password);
        axUserRepository.updateById(axUserUpdateDO);
    }

    @Override
    public void modifyPassword(ModifyPasswordReq modifyPasswordReq) {
        Integer axUid = modifyPasswordReq.getAxUid();
        String phone = modifyPasswordReq.getPhone();
        String code = modifyPasswordReq.getCode();
        String newPassword = modifyPasswordReq.getPassword();

        if (!CommonUtil.checkPhone(phone)) {
            throw new BusinessException(CommonErrorEnum.PARAM_ERROR, "手机号错误");
        }

        AxUserDO axUserDO = axUserRepository.getByMobile(phone);
        if (axUserDO == null) {
            throw new BusinessException(BizErrorEnum.LOGIN_ACCOUNT_NOT_EXIST);
        }
        smsService.verifySms(KaptchaEnum.BusinessCode.MODIFY_PASSWORD, phone, code);

        if (StringUtils.isBlank(axUserDO.getPassword())) {
            SetPasswordReq setPasswordReq = BeanUtil.copyPropertiesByFastJson(modifyPasswordReq, SetPasswordReq.class);
            setPasswordReq.setPassword(newPassword);
            setPassword(setPasswordReq);
            return;
        }
        AxUserDO axUserUpdateDO = new AxUserDO();
        axUserUpdateDO.setAxUid(axUid);
        axUserUpdateDO.setPassword(newPassword);
        axUserRepository.updateById(axUserUpdateDO);
    }

    @Override
    public void forgetPassword(ForgetPasswordReq forgetPasswordReq) {
        String phone = forgetPasswordReq.getPhone();
        String code = forgetPasswordReq.getCode();
        String password = forgetPasswordReq.getPassword();

        AxUserDO axUserDO = axUserRepository.getByMobile(phone);
        if (axUserDO == null) {
            throw new BusinessException(BizErrorEnum.LOGIN_ACCOUNT_NOT_EXIST);
        }

        smsService.verifySms(KaptchaEnum.BusinessCode.FORGET_PASSWORD, phone, code);


        AxUserDO axUserUpdateDO = new AxUserDO();
        axUserUpdateDO.setAxUid(axUserDO.getAxUid());
        axUserUpdateDO.setPassword(password);
        axUserRepository.updateById(axUserUpdateDO);
    }

    @Override
    public void sendSmsForgetPassword(SendMobileCodeReq sendMobileCodeReq) {
        String phone = sendMobileCodeReq.getPhone();

        smsService.sendSms(KaptchaEnum.BusinessCode.FORGET_PASSWORD, phone);
    }

    @Override
    public void sendSmsModifyPassword(SendMobileCodeReq sendMobileCodeReq) {
        String phone = sendMobileCodeReq.getPhone();

        smsService.sendSms(KaptchaEnum.BusinessCode.MODIFY_PASSWORD, phone);
    }
}
